<template>
  <div style="padding: 10px;">
    <el-row >
      <el-col>
        <div style="display: flex;">
          <div>
            <el-form :model="list_params" ref="searchForm"  size="small" :inline="true" @submit.native.prevent="() => handleQuery()">
              <el-form-item label="客户名称" label-width="80px" prop="customerName">
                <el-input v-model="list_params.customerName" placeholder="客户名称搜索" ></el-input>
              </el-form-item>
              <el-form-item label="客户分类" prop="customerClassify">
                <el-select filterable  v-model="list_params.customerClassify" placeholder="请选择客户分类" clearable :style="{width: '100%'}">
                  <el-option v-for="dict in dict.type.customer_classify" :key="dict.value" :label="dict.label"
                             :value="dict.value"  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary"  size="mini"  native-type="submit">搜索</el-button>
                <el-button  size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div style="flex: 1;"></div>
          <div>
<!--            <el-button>删除</el-button>-->
            <el-button @click="handleEnable(0)" >禁用</el-button>
            <el-button @click="handleEnable(1)" >启用</el-button>
            <!-- <el-button @click="handleExport">导出</el-button>
            <el-button>导入</el-button> -->
            <el-button type="primary" @click="addCustom" >添加客户</el-button>
          </div>
        </div>
      </el-col>
      <el-col>
        <div style="margin-top: 10px;">
          <el-table :data="table_data" @selection-change="handleSelectionChange"  :height="table_height">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="ID" prop="id"></el-table-column>
            <el-table-column label="客户名称" prop="customerName"></el-table-column>
            <el-table-column label="客户编号" prop="customerNum"></el-table-column>
            <el-table-column label="客户分类" prop="customerClassify">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.customer_classify" :value="scope.row.customerClassify"/>
              </template>
            </el-table-column>
            <el-table-column label="手机号码" prop="phoneNumber"></el-table-column>
            <el-table-column label="备注" prop="remark"></el-table-column>
            <el-table-column label="创建时间" prop="createTime"></el-table-column>
            <el-table-column label="状态" >
              <template slot-scope="scope" >
              <div style="color: #111928;" v-if="scope.row.enable==1" >启用</div>
              <div style="color: #EB4646;" v-else >禁用</div>
              </template>
            </el-table-column>
            <el-table-column label="操作">

              <template slot-scope="scope" >
              <el-button
                size="mini"
                type="text"
                @click="editInfo(scope.row)"
              >编辑</el-button>
              <el-popconfirm
                title="确定要删除这条记录吗？"
                @confirm="c_delete(scope.row)"
              >
                <el-button
                  slot="reference"
                  size="mini"
                  type="text"
                >删除</el-button>
              </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="list_total>0"
            :total="list_total"
            :page.sync="list_params.pageNum"
            :limit.sync="list_params.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>

    <el-dialog  :visible.sync="add_custom_dialog_show" title="添加客户" width="680px" style="border-radius: 10px;" @close="close">
      <el-form  ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-row   gutter="10">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="formData.customerName" placeholder="请输入客户名称"  show-word-limit clearable
                         :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户编号" prop="customerNum">
              <el-input v-model="formData.customerNum" placeholder="请输入客户编号"  show-word-limit clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户分类" prop="customerClassify">
              <el-select filterable  v-model="formData.customerClassify" placeholder="请选择客户分类" clearable :style="{width: '100%'}">
                <el-option v-for="dict in dict.type.customer_classify" :key="dict.value" :label="dict.label"
                           :value="dict.value"  ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phoneNumber">
              <el-input v-model="formData.phoneNumber" placeholder="请输入手机号" show-word-limit clearable
                         :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="客户类型" prop="customerType">
              <el-radio-group v-model="formData.customerType">
                <el-radio :label="0">个人</el-radio>
                <el-radio :label="1">企业</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input v-model="formData.enterpriseName" placeholder="请输入企业名称" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经办人姓名" prop="agentName">
              <el-input v-model="formData.agentName" placeholder="请输入经办人姓名" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经办人手机号" prop="agentPhone">
              <el-input v-model="formData.agentPhone" placeholder="请输入经办人手机号" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经办人身份证" prop="agentIdCard">
              <el-input v-model="formData.agentIdCard" placeholder="请输入经办人身份证" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="formData.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="上传" prop="pictures" >
              <el-upload ref="field101" :file-list="pictures" :action="uploadImgUrl" :limit="1"
                         :headers="headers" :on-success="field101Success" :on-remove="field101Remove"
                         :before-upload="field101BeforeUpload" list-type="picture-card" accept="image/*">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否启用" prop="enable">
              <el-switch v-model="formData.enable" :active-value="1" :inactive-value="0"  ></el-switch>
            </el-form-item>
          </el-col>
          <el-col>
            <div @click="show_account = !show_account" style="cursor: pointer;background: #F6F6F6;margin:0px -20px;height: 40px;line-height: 40px;padding: 0px 33px;display: flex;justify-content: space-between;margin-bottom: 20px;">
              <div>结算账户信息</div>
              <div  v-show="show_account" >收起<el-icon class="el-icon-arrow-up"></el-icon></div>
              <div v-show="!show_account" >展开<el-icon class="el-icon-arrow-down"></el-icon></div>
            </div>
          </el-col>
          <el-col :span="12" v-show="show_account">
            <el-form-item label="开户银行">
              <el-input v-model="formData.depositBank" style="width:90%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="show_account">
            <el-form-item label="开户名">
              <el-input v-model="formData.accountName" style="width:90%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="show_account">
            <el-form-item label="银行账号">
              <el-input v-model="formData.account" style="width:90%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="title" style="background: #F3F4F6;margin: -20px;padding: 20px;height: 58px;border-radius: 10px;">{{formData.id?'编辑客户':'添加客户'}}</div>
      <div slot="footer" style="display:flex;justify-content: flex-end;">
        <el-button @click="add_custom_dialog_show = false" >取消</el-button>
        <el-button type="primary"  @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list,add,handleEnable,detail,update,del } from "@/api/base/custom";
import { getToken } from "@/utils/auth";
import { getImgs, getUploadFileUrls } from '@/api/common'
import { mapGetters } from 'vuex'
import store from '@/store'
export default {
  name: 'custom',
  dicts:['customer_classify'],
  ...mapGetters([
    'user_id'
  ]),
  data() {
    return {
      table_height:window.innerHeight - 300,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/file/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      add_custom_dialog_show:false,
      edit_custom_dialog_show:false,
      table_data:[],
      show_account:true,
      formData:{
        phoneNumber:'',
        customerName:'',
        customerNum:'',
        customerClassify:'',
        enable:1,
        depositBank:'',
        accountName:'',
        account:'',
        pictures:'',
        createBy:''
      },
      formDataInit:{
        phoneNumber:'',
        customerName:'',
        customerNum:'',
        customerClassify:'',
        enable:1,
        depositBank:'',
        accountName:'',
        account:'',
        pictures:'',
        createBy:''
      },
      pictures:[],
      rules:{
        customerName:[{
          required: true,
          message: '请输入客户名称',
          trigger: 'blur'
        }],
        customerNum:[{
          required: true,
          message: '请输入客户编号',
          trigger: 'blur'
        }],
        customerClassify:[{
          required: true,
          message: '请选择客户分类',
          trigger: 'change'
        }],
        phoneNumber: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          pattern: /^1(3|4|5|7|8|9)\d{9}$/,
          message: '手机号格式错误',
          trigger: 'blur'
        }],
        customerType:[{
          required: true,
          message: '请选择可客户类型',
          trigger: 'change'
        }]
      },
      ids:[],
      list_params:{
        pageNum:1,
        pageSize:10,
        customerName:'',
        customerClassify:''
      },
      list_total:0
    }
  },
  created() {
    this.getList()
  },
  methods:{
    getList(){
      list(this.list_params).then(res => {
        this.table_data = res.rows;
        this.list_total = res.total;
        console.log(res.rows)
      })
    },
    addCustom(){
      this.formData = {
        ...this.formDataInit
      };
      this.add_custom_dialog_show = true;
    },
    close(){
      this.pictures = []
      this.$refs['elForm'].resetFields();
      this.add_custom_dialog_show = false
    },
    handleConfirm(){
        this.$refs['elForm'].validate((valid)=>{
          if (valid){
            this.formData.createBy = store.getters.user_id;
            this.formData.enable = this.formData.data_enable?0:1
            console.log(this.formData)

            this.add_custom_dialog_show = false
            if (this.formData.id){
              let formData = {
                ...this.formData,
                xnfSettleAccountsList:[{
                  'depositBank':this.formData.depositBank,
                  'accountName':this.formData.accountName,
                  'account':this.formData.account
                }
                ]
              }
              update(formData).then(res=>{
                this.getList()
                this.$message.success('修改成功')
              })
            }else{
              let formData = {
                ...this.formData,
                xnfSettleAccountsList:[{
                  'depositBank':this.formData.depositBank,
                  'accountName':this.formData.accountName,
                  'account':this.formData.account
                }
                ]
              }
              add(formData).then(res=>{
                this.getList()
                this.$message.success('添加成功')
              })
            }
            this.$refs['elForm'].resetFields();
          }
        });
    },
    handleUpdate(){
        this.$refs['elEditForm'].validate((valid)=>{
          if (valid){
            let formData = this.formEditData;
            formData.createBy = store.getters.user_id;

            this.edit_custom_dialog_show = false
            console.log(formData)
            update(formData).then(res=>{
              this.getList()
            })
            this.$refs['elEditForm'].resetFields();
          }
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleExport() {
      this.download('xnf/customer/export', {
        ...this.queryParams
      }, `customer${new Date().getTime()}.xlsx`)
    },
    handleEnable(enable){
      if (this.ids.length == 0){
        this.$message({
          message: '请选择要操作的数据',
          type: 'warning'
        });
        return;
      }
      handleEnable(this.ids,enable).then(res => {
        this.getList()
      })
    },
    editInfo(row){
      let id = row.id;
      detail(id).then(res => {
        this.formData = {
          ...res.data,
          customerClassify: (res.data.customerClassify)+'',
        }
        if (res.data.xnfSettleAccountsList.length > 0){
          this.formData = {
            ...this.formData,
            xnfSettleAccountsList:undefined,
            depositBank: res.data.xnfSettleAccountsList[0].depositBank,
            accountName: res.data.xnfSettleAccountsList[0].accountName,
            account: res.data.xnfSettleAccountsList[0].account
          }
        }
        if (res.data.pictures!=''){
          getImgs(res.data.pictures).then(res => {
            this.imgs = getUploadFileUrls(res.data);
            this.pictures = this.imgs;
          })
          // this.pictures = [JSON.parse(res.data.pictures)]
        }
        this.add_custom_dialog_show = true
      })
    },
    c_delete(row){
      let id = row.id;
      del(id).then(res=>{
        this.getList()
      })
    },
    field101BeforeUpload(e){
    },
    field101Success(e){
      console.log(e)
      this.formData = {
        ...this.formData,
        pictures:e.data.id
      }
    },
    field101Remove(){
      this.formData = {
        ...this.formData,
        pictures:''
      }
    },
    handleQuery(){
      this.list_params.pageNum = 1;
      this.getList();
    },
    resetQuery(){
      this.$refs['searchForm'].resetFields();
      this.handleQuery()
    },
  }
}
</script>

<style scoped>

</style>
